import axios from 'axios'
import qs from 'qs'
import "babel-polyfill";
import Vue from 'vue'
import utilCommon from '@/util/common'
require('es6-promise').polyfill();
import globalConfig from '@/config'
// axios 配置
axios.defaults.timeout = 60000;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
axios.defaults.baseURL = globalConfig.requireUrl;
import router from '../router'


var u = navigator.userAgent,
	isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),
	isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;


function isWinxin() {
	const ua = window.navigator.userAgent.toLowerCase();
	if (ua.match(/MicroMessenger/i) == 'micromessenger') {
		return true;
	} else {
		return false;
	}
};

function getlocalStorage(storeName, storeData) {
	var storeName = localStorage.getItem(storeName);
	var storeArr = JSON.parse(storeName)
	return storeArr;
};

function setlocalStorage(storeName, storeData) {
	var storeStr = JSON.stringify(storeData);
	var storeArr = window.localStorage.setItem(storeName, storeStr);
	return storeArr;
}

//POST传参序列化
axios.interceptors.request.use((config) => {
	// config.headers['If-Modified-Since'] = '0'
	if (config.method === 'post') {
		config.data = qs.stringify(config.data);
	}
	return config;
}, (error) => {
	console.log('错误的传参');
	return Promise.reject(error);
});

//返回状态判断
axios.interceptors.response.use((res) => {
	if (!res.data.ok) {
		if (res.data.msg == '请登录后进行此操作') {

		}
		return Promise.reject(res);
	}

	return Promise.reject(res);
}, (error) => {
	console.log('网络异常');
	if (error.response.status == '403' && !utilCommon.ENV()) {
		router.push({
			path: "NewAuthority"
		})
	}
	return Promise.reject(error);
});

export function fetch(url, method, params) {
	return new Promise((resolve, reject) => {
		console.log('url:' + url)
		console.log(params)

		if (method == 'post') {
			axios.post(url, params)
				.then(response => {
					resolve(response.data);
				}, err => {
					reject(err);
				})
				.catch((error) => {
					reject(error)
				})
		} else if (method == 'get') {
			axios.get(url, {
				params: params
			})
				.then(response => {
					console.Console.log(response)
					resolve(response.data);
				}, err => {
					reject(err);
				})
				.catch((error) => {
					reject(error)
				})
		} else if (method == 'delete' || method == 'put') {
			if (method == 'put') {
				axios.put(url, {
					params: params
				})
					.then(response => {
						resolve(response.data);
					}, err => {
						reject(err);
					})
					.catch((error) => {
						reject(error)
					})
			} else {
				axios.delete(url, {
					params: params
				})
					.then(response => {
						resolve(response.data);
					}, err => {
						reject(err);
					})
					.catch((error) => {
						reject(error)
					})
			}

		}
	})
}

export default {

	/**
     * 微信支付
     */

	handleChooseWXPay(params) {
		return fetch('/commodity/v1/wxpay/', 'post', params)
	},
	/**
     * 获取商家信息
     */
	getMerchantInfo(params) {
		return fetch('/commodity/v1/getMerchantInfo/', 'get', params)
	},
	/**
     * 商家注册
     */
	regiMerchant(params) {
		return fetch('/commodity/v1/regiMerchant/', 'post', params)
	},
	/**
     * 获取商品信息
     */
	getGoodsInfo(params) {
		return fetch('/commodity/v1/getGoodsInfo/', 'get', params)
	},

    /**
     * 授权
     */
	wxAuthorization(params) {
		return fetch('/api/wechat/authorization', 'get', params)
	},
    /**
     * 获取微信证书
     */
	wxSignature(params) {
		return fetch('/user/v1/wxqx/', 'get', params)
	},

    /**
     * 订单管理
     */
	//用户全部订单
	myOrderlist(params) {
		return fetch('/indent/v1/ajax/machineOrder/', 'get', params)
	},
	//待付款订单
	myQueryOrder(params) {
		return fetch('/indent/v1/ajax/queryorder/', 'get', params)
	},
	//待发货订单
	myOrderSendgoods(params) {
		return fetch('/indent/v1/ajax/sendgoods/', 'get', params)
	},
	//待收货订单
	myOrderWaitgoods(params) {
		return fetch('/indent/v1/ajax/waitgoods/', 'get', params)
	},
	//机器扫码支付订单
	myMachineOrderlist(params) {
		return fetch('/indent/v1/ajax/machineOrder/', 'get', params)
	},

	/**
	 * 地址管理
	 */
	//收货地址列表
	takeaddressList(params) {
		return fetch('/user/v1/ajax/takeaddress/', 'get', params)
	},

	/**
	 * 生成订单
	 */
	setPurchasekill(params) {
		return fetch('/commodity/v1/purchasekill/', 'post', params)
	},

	// 获取收货地址
	getaddress(params) {
		return fetch('/commodity/v1/getaddress/', 'get', params)
	},
	//新增收货地址
	takeaddressAdd(params) {
		return fetch('/user/v1/ajax/takeaddress/', 'post', params)
	},
	//删除收货地址
	takeaddressRemove(params) {
		return fetch('/user/v1/ajax/deleteAddress/', 'post', params)
	},
	//编辑收货地址
	takeaddressEdit(params) {
		return fetch('/user/v1/ajax/updateAddress/', 'post', params)
	},

	//推荐编号和二维码
	userQrcode(params) {
		return fetch('/user/v1/myId/', 'get', params)
	},

	/**
	 * 登录注册
	 */

	//获取验证码
	entrySendcode(params) {
		return fetch('/user/v1/sms/', 'post', params)
	},
	//注册
	userRegister(params) {
		return fetch('/register/v1/', 'post', params)
	},
	//登录
	userLogin(params) {
		return fetch('/login/v1/', 'post', params)
	},
	//绑定手机号
	bindingPhone(params) {
		return fetch('/user/v1/chagePhone/', 'post', params)
	},


	//分类
	apiClassify(params) {
		return fetch('/commodity/v1/apparatus/goods/title/', 'get', params)
	},
	//分类 左边菜单
	apiClassifyType(params) {
		return fetch('/commodity/v1/apparatus/goods/type/', 'get', params)
	},

	//店铺查询
	apiShopaddress(params) {
		return fetch('/machine/v1/ajax/shopaddress/', 'get', params)
	},
	//商品列表
	apparatusGoodsList(params) {
		return fetch('/commodity/v1/apparatus/goods/type/', 'get', params)
	},
	//banner图列表
	indexBannerList(params) {
		return fetch('/commodity/v1/apparatus/index/banner/', 'get', params)
	},

	//抽奖活动 提交码获取奖品
	luckyWheelGetLottery(params) {
		return fetch('/choujiang/v1/', 'post', params)
	},
	//抽奖活动 提交领取奖品信息
	luckyWheelGetLotteryAddr(params) {
		return fetch('/choujiang/v1/', 'get', params)
	},

	//个人中心 我的团队
	getMyTeam(params) {
		return fetch('/user/v1/mytem/', 'get', params)
	},
	//个人中心 我的下线
	getMySmalltem(params) {
		return fetch('/user/v1/mysmalltem/', 'get', params)
	},
	//个人中心  我的推荐人
	getMySuperior(params) {
		return fetch('/user/v1/mySuperior/', 'get', params)
	},
	//个人中心  我的钱库余额
	getMyBalance(params) {
		return fetch("/user/v1/myBalance/", 'get', params)
	},
	//个人中心  我的钱库  提现记录
	getMyRecordList(params) {
		return fetch("/user/v1/myBalance/", 'get', params)
	},
	//个人中心  我的钱库  提现操作
	getMyRecord(params) {
		return fetch("/user/v1/myBalance/", 'post', params)
	},

	//本地的测试数据
	staticCeshi(params) {
		return fetch("http://" + window.location.host + "/static/ceshi.json", 'get', params)
	},


	//商品详情
	getGoodsDetail(params) {
		return fetch("/commodity/v1/detailsGoods/", 'get', params)
	},
	//商品评论
	getGoodsComment(params) {
		return fetch("/commodity/v1/goodscomment/", 'get', params)
	},
	// 获取所有评价
	getGoodsVal(params) {
		return fetch("/user/v1/machineComment/", 'get', params)
	},

	//消息列表
	getMsgList(params) {
		return fetch("/information/v1/GlobalInfo/", 'get', params)
	},
	//点击消息改为已读状态
	xinxiyiduViews(params) {
		return fetch("/information/v1/xinxiyiduViews/", 'get', params)
	},

	//机器评论
	machineComment(params) {
		return fetch("/user/v1/machineComment/", 'post', params)
	},
	//我的机器评论列表
	machineCommentList(params) {
		return fetch("/user/v1/machineComment/", 'get', params)
	},

	//商品banner图
	getCommodityPhotos(params) {
		return fetch("/commodity/v1/goodsPhoto/", 'get', params)
	},

	//一键修改用户等级
	moduserlevel(params) {
		return fetch("user/v1/moduserlevel/", 'post', params)
	},

	//一键修改用户上下级
	moduserpath(params) {
		return fetch("user/v1/moduserpath/", 'post', params)
	},
	// 获取二维码
	getWxerweima(params) {
		return fetch("information/v1/wxerweima/", 'get', params)
	},
	// 获取提现记录
	getTxRecord(params) {
		return fetch("money/v1/txRecord/", 'get', params)
	},
	// 获取已提现金额
	gettxCount(params) {
		return fetch("money/v1/txCount/", 'get', params)
	},
	// 查询能否开店
	selectAddress(params) {
		return fetch("ally/v1/selectAddress/", 'get', params)
	},
	// 省、市、区/县、镇四级联动
	fouraddress(params) {
		return fetch("threeaddress/v1/fouraddress/", 'get', params)
	},
	// 三级城市联动
	threeaddressCity(params) {
		// alert(window.location.href.split('#')[0]+'/static/pca-code.json')
		return fetch('threeaddress/v1/address/', 'get', params)
	},
	// 获取用户余额支出详情
	getbalanceSubRecord(params) {
		return fetch("money/v1/balanceSubRecord/", 'get', params)
	},
	// 获取用户余额收入
	getbalanceAddRecord(params) {
		return fetch("money/v1/balanceAddRecord/", 'get', params)
	},

	// 获取我的团队总人数
	getmyteam(params) {
		return fetch("user/v1/getmyteam/", 'get', params)
	},

	// 获取我的下级总人数
	getnextteam(params) {
		return fetch("user/v1/getnextteam/", 'get', params)
	},

	// 我的团队
	mytem(params) {
		return fetch("user/v1/mytem/", 'get', params)
	},
	// 获取用户授权书
	getauthorize_get(params) {
		return fetch("information/v1/userauth/", 'get', params)
	},
	// 获取用户授权书
	getauthorize_post(params) {
		return fetch("information/v1/userauth/", 'post', params)
	},
	// 用户授权审核
	audituser(params) {
		return fetch("information/v1/audituser/", 'get', params)
	},
	// 修改用户授权申请授权状态
	modifieraudit(params) {
		return fetch("information/v1/modifieraudit/", 'get', params)
	},
	// //获取验证码2
	// entrySendcode2(params) {
	// 	return fetch('http://ceshi.sailafeinav.com/user/v1/sms/', 'post', params)
	// },
	// 更新用户的微信推广二维码
	updataewm(params) {
		return fetch("information/v1/updataewm/", 'post', params)
	},
	// 获取秒杀区商品
	getGoodsInfo(params) {
		return fetch("/commodity/v1/getGoodsInfo/", 'get', params)
	},

	// 获取秒杀区商品
	getkillorder(params) {
		return fetch("/commodity/v1/getkillorder/", 'get', params)
	},

	// 取消订单列表
	cancelorder(params) {
		return fetch("/commodity/v1/cancelorder/", 'post', params)
	},

	// 获取团队购买数量
	getteamsum(params) {
		return fetch("/commodity/v1/getteamsum/", 'get', params)
	},
	// 查询物流
	getexpressinfo(params) {
		return fetch("/commodity/v1/getexpressinfo/", 'post', params)
	},

	// 修改订单
	purchasekill(params) {
		return fetch("/commodity/v1/purchasekill/", 'post', params)
	},

	// 获取商家信息
	getMerchantInfo(params) {
		return fetch("/commodity/v1/getMerchantInfo/", 'get', params)
	},

	// 联创出货
	delivergoods(params) {
		return fetch("/commodity/v1/delivergoods/", 'post', params)
	},

	// 联创进货
	addinventory(params) {
		return fetch("/commodity/v1/addinventory/", 'post', params)
	},

	// 获取同为联创的下级
	subuser(params) {
		return fetch("/commodity/v1/subuser/", 'get', params)
	},

	// 联创转让货物
	transfergoods(params) {
		return fetch("/commodity/v1/transfergoods/", 'post', params)
	},

	// 联创转让货物
	orderlist(params) {
		return fetch("/commodity/v1/orderlist/", 'get', params)
	},


	// 商家注册
	regiMerchant(params) {
		return fetch("/commodity/v1/regiMerchant/", 'post', params)
	},

	// 添加购物车订单
	addcart(params) {
		return fetch("/commodity/v1/addcart/", 'post', params)
	},

	// 添加购物车订单
	getcart(params) {
		return fetch("/commodity/v1/getcart/", 'get', params)
	},

	// 修改购物车订单
	modifycart(params) {
		return fetch("/commodity/v1/modifycart/", 'post', params)
	},

	// 删除购物车订单
	removecart(params) {
		return fetch("/commodity/v1/removecart/", 'post', params)
	},


	// 获取待处理订单列表!!!!!!
	orderlist(params) {
		return fetch("/commodity/v1/orderlist/", 'get', params)
	},

	// 获取待处理订单列表
	pendingorder(params) {
		return fetch("/commodity/v1/pendingorder/", 'get', params)
	},
	// 获取已完成订单列表
	finishedorder(params) {
		return fetch("/commodity/v1/finishedorder/", 'get', params)
	},

	// 联创使用积分进货
	usercore(params) {
		return fetch("/commodity/v1/usercore/", 'post', params)
	},

	// 代理商确认收货
	takedeliver(params) {
		return fetch("/commodity/v1/takedeliver/", 'post', params)
	},

	// ===============外包
	// 获取商品
	getProductDetail(id, params) {
		return fetch("indent/v1/goodssearch/" + id + "/", 'post', params)
	},

	// 获取团队等级数据
	getuserlevel(params) {
		return fetch("/user/v1/getuserlevel/", 'get', params)
	},

	// 获取订单列表接口
	getorderstatus(params, _url) {
		return fetch(_url ? _url : "/indent/v1/getorderstatus/", 'get', params)
	},
	// 获取首页功能区
	getRegion(params) {
		return fetch("/commodity/v1/index_region/", 'get', params)
	},
	// 获取首页轮播图接口
	getBanner(params) {
		return fetch("/commodity/v1/goodsPhoto/", 'get', params)
	},
	// 获取商品分类
	getgoodsclass(params) {
		return fetch("/indent/v1/getgoodsclass/", 'get', params)
	},
	// 获取商品列表
	getProductList(params) {
		return fetch("/indent/v1/getgoodsinfo/", 'get', params)
	},
	// 搜索商品
	goodssearch(params) {
		return fetch("/indent/v1/goodssearch/", 'get', params)
	},
	// 获取购物车数据
	goodscarcrud(_mthods, params, _shopId) {
		let url = _shopId ? `indent/v1/goodscarcrud/${_shopId}/` : 'indent/v1/goodscarcrud/'
		return fetch(url, _mthods, params)
	},

	// 获取商品所有评级
	getGoodscomment(goodId, params, _url) {
		return fetch(_url ? _url : `/indent/v1/goodscomment/${goodId}/`, 'get', params)
	},

	// 搜索商品
	goodscommentinfo(uid, params) {
		return fetch(`indent/v1/goodscommentinfo/${uid}/`, 'get', params)
	},

	// 统一下单接口
	goodsbuypay(params) {
		return fetch("/indent/v1/goodsbuypay/", 'post', params)
	},
	// 获取微信需要的参数
	gzhwxpayparams(params) {
		return fetch("/machine/v1/gzhwxpayparams/", 'post', params)
	},
	// 发布评价
	setGoodscomment(goodId, params) {
		return fetch(`/indent/v1/goodscomment/${goodId}/`, 'post', params)
	},
	// 积分支付
	getIntegralPay(params) {
		return fetch("/machine/v1/integralpay/", 'post', params)
	},
	// 验证密码是否正确
	userpayinfo(params) {
		return fetch("/user/v1/userpayinfo/", 'post', params)
	},
	// 获取订单数量
	getOrderNum(params) {
		return fetch("/indent/v1/getorderstatus/", 'get', params)
	},
	// 取消订单
	cancelorder(params) {
		return fetch("/indent/v1/cancelorder/", 'post', params)
	},
	// 订单详情
	getOrderDetail(params) {
		return fetch("/indent/v1/getorderstatus/", 'get', params)
	},

	// 查询是否有密码
	userpayinfo(params) {
		return fetch("/user/v1/userpayinfo/", 'post', params)
	},

	// 确认收货
	suregetgoods(params) {
		return fetch("/indent/v1/suregetgoods/", 'post', params)
	},
	// 处理微信取消支付
	wxCancel(params) {
		return fetch("/indent/v1/goodscancelpay/", 'post', params)
	},

	// 发送验证码
	smspost(params) {
		return fetch("/user/v1/sms/", 'post', params)
	},

	// 验证验证码
	smsget(params) {
		return fetch("/user/v1/sms/", 'get', params)
	},

	// 用户绑定支付密码
	bindUserPay(params) {
		return fetch("/user/v1/userpayinfo/", 'post', params)
	},

	// 刷新用户信息
	newuserinfo(params) {
		return fetch("/user/v1/newuserinfo/", 'get', params)
	},

	// 查看物流
	getLogistics(params, _url) {
		return new Promise((resolve, reject) => {
			axios({
				method: 'get',
				url: _url,
				params: params,
				headers: {
					Authorization: 'APPCODE b673113f85b9494a824ba17780baacf3'
				}

			}).then(err => {

				reject(err)
			}).catch(res => {
				resolve(res)
			}).then(res => {
				// console.log(res)
				// console.log('返回')
				// resolve(res)
			})


		})
	},

	// 设置为默认地址
	setdefaultadd(params) {
		return fetch("/user/v1/setdefaultadd/", 'post', params)
	},
	getFreight(params) {
		return fetch("/indent/v1/goodsbuypay/", 'get', params)
	},
	// 获取收入支出明细
	getBalanceRecord(params) {
		return fetch("/money/v1/balanceRecord/", 'get', params)
	},
	// 获取礼包区
	getGiftData(params) {
		return fetch("/indent/v1/upgradeKAC/", 'get', params)
	},
	// 判断是否能购买商品
	getUpgrade(params) {
		return fetch("/indent/v1/upgradeKAC/", 'post', params)
	},
	// 获取商品的服务条款
	getGoodsService(params) {
		return fetch("/indent/v1/getgoodsservice/", 'get', params)
	},
	// 分享图片
	goodshare(params) {
		return fetch("/indent/v1/goodshare/", 'get', params)
	},
	// 认证资料
	setAudit(params) {
		return fetch("/user/v1/agencyid/", 'post', params)
	},
	// 查看是否认证
	agencyid(params) {
		return fetch("/user/v1/agencyid/", 'get', params)
	}

}
